/**
 * 在生产环境下 判断https的跳转
 */
export function jumpingHttps() {
  console.log(process.env.NODE_ENV);
  if (process.env.NODE_ENV !== 'production') {
    return;
  }
  const { protocol, host } = window.location;
  if (protocol.indexOf('https') === -1) {
    window.location.replace(`https://${host}`);
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function getQueryObject(param) {
  const url = param == null ? window.location.href : param;
  const search = url.substring(url.lastIndexOf('?') + 1);
  const obj = {};
  const reg = /([^?&=]+)=([^?&=]*)/g;
  search.replace(reg, (rs, $1, $2) => {
    const name = decodeURIComponent($1);
    let val = decodeURIComponent($2);
    val = String(val);
    obj[name] = val;
    return rs;
  });
  return obj;
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = url.split('?')[1];
  if (!search) {
    return {};
  }
  return JSON.parse(
    `{"${
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"')
        .replace(/\+/g, ' ')
    }"}`,
  );
}
